<template>
  <div v-loading.fullscreen.lock="loading">
    <div class="page-header">
      <p>{{ $route.query.examtitle }} - {{ $route.query.sujectname }}</p>
      <div class="page-header-back" @click="$router.back()">退出</div>
    </div>

    <div class="container">
      <div class="container-left">
        <div class="count">
          <el-select
            v-model="form.classId"
            style="width: 100%"
            placeholder="请选择评阅班级"
            @change="formChange"
          >
            <el-option
              v-for="item in classlist"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select
            v-model="form.questionId"
            style="width: 100%"
            placeholder="请选择评阅题组"
            @change="formChange"
          >
            <el-option
              v-for="item in questionList"
              :key="item.id"
              :label="
                item.name +
                item.questionNumber +
                (item.smallQuestionNumber ? '-' + item.smallQuestionNumber : '')
              "
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <el-table
          border
          :data="tableData"
          @row-click="rowClick"
          :cell-class-name="isTableActive"
        >
          <el-table-column
            label="序号"
            width="60"
            align="center"
            type="index"
          />
          <el-table-column label="AI分数" align="center" prop="aiScore" />
          <el-table-column label="批阅分数" align="center">
            <template slot-scope="scope">
              {{ scope.row.isReview ? scope.row.teacherScore : "" }}
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="container-right" ref="containerRight">
        <el-button class="allBlank" type="primary" @click="dialogVisible = true"
          >智批分数显示</el-button
        >
        <div class="scroll-box" ref="scrollBox">
          <div class="yc-box">
            <img
              class="ycxx-image"
              v-if="tableData.length"
              :src="imgSrc"
              alt=""
            />
            <div
              v-if="imgSrc"
              class="yc-item"
              :style="`width:${tkxx.w}px;height:${tkxx.h}px;top:${tkxx.y}px;left:${tkxx.x}px`"
            ></div>
          </div>
        </div>
      </div>
      <div class="page-bottom">
        <p>已批阅/总题数:{{ markCount }}/{{ tableData.length }}</p>

        <el-input
          v-model="score"
          style="width: 100px; margin-right: 15px"
          @keyup.enter.native="submit"
        ></el-input>

        <el-button
          @click="submit"
          type="success"
          :disabled="tableData.length == 0"
          >提交 (Enter)</el-button
        >
      </div>
    </div>

    <el-dialog title="智批分数显示" :visible.sync="dialogVisible" width="450px">
      <el-form label-width="auto">
        <el-form-item label="是否显示">
          <el-select v-model="isShowScore">
            <el-option label="显示" :value="true"></el-option>
            <el-option label="隐藏" :value="false"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="open">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "../../request/api.js";
export default {
  data() {
    return {
      loading: false,
      tableData: [],

      classlist: [],
      questionList: [],
      form: {
        classId: "",
        questionId: "",
      },
      score: "",

      activeId: 0,

      dialogVisible: false,
      isShowScore: true,
    };
  },
  computed: {
    imgSrc: function () {
      if (this.tableData.length > 0) {
        return this.tableData.find((item) => {
          return this.activeId == item.id;
        }).paperImages[this.tkxx.page - 1].filePath;
      } else {
        return "";
      }
    },
    tkxx: function () {
      return this.questionList.find((item) => {
        return item.id == this.form.questionId;
      });
    },
    markCount: function () {
      return this.tableData.filter((item) => {
        return item.isReview;
      }).length;
    },
  },
  created() {
    get("/addscore/check", {}).then((res) => {
      if (res.data) {
        get("/exampaper/classlist", { examId: this.$route.query.examid })
          .then((res) => {
            if (res.code == 200) {
              this.classlist = res.data;
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((error) => {
            this.$message.error(error.message);
          });
        get("/addscore/list", { examcourseId: this.$route.query.subjectid })
          .then((res) => {
            if (res.code == 200) {
              this.questionList = res.data;
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((error) => {
            this.$message.error(error.message);
          });
      } else {
        this.$router.replace("/homework");
      }
    });
  },
  methods: {
    formChange() {
      if (this.form.classId && this.form.questionId) {
        this.loading = true;
        get("/addscore/students", {
          classId: this.form.classId,
          questionId: this.form.questionId,
          examId: this.$route.query.examid,
        })
          .then((res) => {
            if (res.code == 200) {
              this.tableData = res.data;
              this.activeId = res.data[0].id;

              setTimeout(() => {
                this.scroll();
              }, 300);
            } else {
              this.$message.error(res.message);
            }
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },

    scroll() {
      let x = this.tkxx.x - this.$refs.containerRight.offsetWidth / 2;
      let y = this.tkxx.y - this.$refs.containerRight.offsetHeight / 2;

      this.$refs.scrollBox.scrollTo({
        top: y,
        left: x,
      });
    },

    rowClick(row) {
      if (row.id != this.activeId) {
        this.activeId = row.id;
      }
    },

    isTableActive(row) {
      if (row.row.id == this.activeId) {
        return "table-active";
      } else {
        return "";
      }
    },

    isValidNumber(str) {
      const trimmedStr = str.trim();
      if (trimmedStr === "") return false;
      const numRegex = /^-?\d+(\.\d)?$/;
      if (!numRegex.test(trimmedStr)) {
        return false;
      }
      const num = parseFloat(trimmedStr);
      const twiceNum = num * 2;
      return Number.isInteger(twiceNum);
    },

    submit() {
      if (this.loading || this.tableData == 0) {
        return;
      }
      if (!this.isValidNumber(this.score)) {
        this.$message.error("您输入的分数格式错误");
        return;
      }
      if (this.score > this.tkxx.fullMarks) {
        this.$message.error("您输入的分数大于满分");
        return;
      }

      this.loading = true;
      post("/addscore/update", {
        questionId: this.form.questionId,
        score: this.score,
        paperId: this.activeId,
      })
        .then((res) => {
          if (res.code == 200 && res.data) {
            let tableData = JSON.parse(JSON.stringify(this.tableData));

            let index = tableData.findIndex((item) => {
              return item.id == this.activeId;
            });

            tableData[index].teacherScore = this.score;
            tableData[index].isReview = true;

            this.tableData = tableData;

            this.score = "";

            if (tableData[index + 1]) {
              this.activeId = tableData[index + 1].id;
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },

    open() {
      this.loading = true;
      post("/addscore/showscore", {
        examId: this.$route.query.examid,
        isShowScore: this.isShowScore,
      })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success("操作成功");
            this.$router.back();
          } else {
            this.$message.error(res.message);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style >
.table-active {
  background: #288f411a;
}
</style>
<style scoped>
.page-header {
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  background: #1a48db;
  font-size: 16px;
  padding: 0 24px 0 44px;
}
.page-header p {
  margin: 0;
}
.page-header .page-header-back {
  width: 76px;
  height: 32px;
  background: #ffffff;
  border-radius: 16px;
  line-height: 32px;
  text-align: center;
  color: #1a48db;
  font-size: 16px;
  cursor: pointer;
}
.page-header .page-header-back:active {
  opacity: 0.8;
}
.container {
  height: calc(100vh - 50px);
  display: flex;
}
.container .container-left {
  width: 256px;
  height: 100%;
  background: #ffffff;
  overflow: auto;
}
.container .container-left .count {
  width: 100%;
  padding: 5px 15px;
  box-sizing: border-box;
}
.container .container-left .count div {
  width: 50%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 14px;
}
.container .container-left .count .ycl {
  color: #e93a40;
  background: #e93a401a;
}
.container .container-left .count .wcl {
  color: #288f41;
  background: #288f411a;
}
.container .container-right {
  width: calc(100% - 256px);
  height: calc(100vh - 100px);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.container .container-right .allBlank {
  position: absolute;
  right: 20px;
  top: 20px;
  z-index: 9;
}
.container .container-right .scroll-box {
  width: 100%;
  height: 100%;
  overflow: auto;
  background: #f5f5f5;
}
.container .container-right .scroll-box .yc-box {
  position: relative;
}
.container .container-right .scroll-box .yc-box .yc-item {
  border: 3px solid #f56c6c;
  background: rgba(245, 108, 108, 0.2);
  position: absolute;
  cursor: pointer;
}
.container .container-right .scroll-box .yc-box .yc-item.active {
  background: transparent;
}
.container .container-right .scroll-box .yc-box .yc-item.processed {
  border-color: #13ce66;
  background: transparent;
}
.page-bottom {
  width: calc(100vw - 256px);
  justify-content: flex-start;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  height: 50px;
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 0 20px;
  background: rgba(255, 255, 255, 0.84);
  -webkit-box-shadow: 0 4px 16px 0 #5572921a;
  box-shadow: 0 4px 16px 0 #5572921a;
  z-index: 9;
  box-sizing: border-box;
}
.page-bottom p {
  color: #606266;
  font-size: 14px;
  margin-right: 20px;
}
.page-bottom .yc-answer-box {
  display: flex;
  margin: 0 20px;
}
.page-bottom .yc-answer-box .yc-answer-item {
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background: #f2f3ff;
  cursor: pointer;
  color: #1a48db;
}
.page-bottom .yc-answer-box .yc-answer-item + .yc-answer-item {
  margin-left: 10px;
}
.page-bottom .yc-answer-box .yc-answer-item.active {
  color: #ffffff;
  background: #1a48db;
}
.page-bottom .yc-answer-box .yc-answer-item.danger {
  background: #fdeeef;
  color: #ec555a;
  width: 100px;
}
.page-bottom .yc-answer-box .yc-answer-item.danger.active {
  background: #ec555a;
  color: #fff;
}
</style>
